/**
Simple Server for web api test.
*/
/**Connect是一个node中间件（middleware）框架。
如果把一个http处理过程比作是污水处理，中间件就像是一层层的过滤网。
每个中间件在http处理过程中通过改写request或（和）response的数据、状态，实现了特定的功能。
中间件就是类似于一个过滤器的东西，在客户端和应用程序之间的一个处理请求和响应的的方法。*/

var connect = require('connect');  //创建连接
var bodyParser = require('body-parser');   //body解析

var app = connect()
    .use(bodyParser.json())   //JSON解析
    .use(bodyParser.urlencoded({extended: true}))
	//use()方法还有一个可选的路径字符串，对传入请求的URL的开始匹配。
	//use方法来维护一个中间件队列
	.use(function (req, res, next) {
		//跨域处理
		// Website you wish to allow to connect
        res.setHeader('Access-Control-Allow-Origin', '*');  //允许任何源
        // Request methods you wish to allow
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');  //允许任何方法
        // Request headers you wish to allow
        res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,X-Session-Token');   //允许任何类型
		res.writeHead(200, {"Content-Type": "text/plain;charset=utf-8"});    //utf-8转码
		next();  //next 方法就是一个递归调用
	})
	.use('/info', function(req, res, next) {
		//response 响应   request请求
		// 中间件
		console.log(req.body);
		//console.log(req.originalUrl, req.url);
		var data={
                "code": "200",
                "msg": "success",
                "result": [{
					"title": "banner1",
					"url": "img/banner1.png"
				}, {
					"title": "banner2",
					"url": "img/banner2.png"
				}, {
					"title": "banner3",
					"url": "img/banner3.png"
				}, {
					"title": "banner4",
					"url": "img/banner4.png"
				}]
				
				
            }
            res.end(JSON.stringify(data));
			next();      //
	})
	.use('/info2', function(req, res, next) {
		
		console.log(req.body);
		var data = [{
			"cls": "es ht-peixun",
			"id": 1,
			"name": "在线培训"
		}, {
			"cls": "es ht-chanye",
			"id": 2,
			"name": "集团介绍"
		}, {
			"cls": "es ht-jieshao",
			"id": 3,
			"name": "集团产品"
		}, {
			"cls": "es ht-xinwen",
			"id": 4,
			"name": "集团新闻"
		}, {
			"cls": "es ht-peixun",
			"id": 5,
			"name": "在线培训"
		}, {
			"cls": "es ht-chanye",
			"id": 6,
			"name": "集团介绍"
		}, {
			"cls": "es ht-jieshao",
			"id": 7,
			"name": "集团产品"
		}, {
			"cls": "es ht-xinwen",
			"id": 8,
			"name": "集团新闻"
		}];
		// data.push(req.body);
		
		res.end(JSON.stringify(data));
		next();
	})
	.use('/info3', function(req, res, next) {
		//response 响应   request请求
		// 中间件
		console.log(req.body);
		//console.log(req.originalUrl, req.url);
		var data= ["广告投放1","广告投放2","广告投放3","广告投放4","广告投放5"];
            res.end(JSON.stringify(data));
			next();      //
		})
	.use('/info4', function(req, res, next) {
		//response 响应   request请求
		// 中间件
		console.log(req.body);
		//console.log(req.originalUrl, req.url);
		var data={
                "code": "200",
                "msg": "success",
                "result": ["广告投放1","广告投放2","广告投放3","广告投放4","广告投放5"]
            }
            res.end(JSON.stringify(data));
			next();      //
	})
	.use('/info_img', function(req, res, next) {
		//response 响应   request请求
		// 中间件
		var data={
                "code": "200",
                "msg": "success",
                //"result": "http://img4.imgtn.bdimg.com/it/u=3900663449,3280179767&fm=214&gp=0.jpg"
				"result":""
			}
            res.end(JSON.stringify(data));
			next();      
	})
    .use('/form_info', function(req, res, next) {
		//response 响应   request请求
		// 中间件
		console.log(req.body);
		var data={
                "code": "200",
                "msg": "success",
                "result": []
            }
         res.end(JSON.stringify(data));
	})
	.use('/list', function(req, res, next) {
		var data={
                "code": "200",
                "msg": "success",
                "result": [{name:'手机1',state:'未采购'},
                    {name:'手机2',state:'未采购'},
                    {name:'手机3',state:'已采购'},
                    {name:'手机4',state:'未采购'}]
            }
            res.end(JSON.stringify(data));
			next();      //
	})
	
	
	.listen(3000);
console.log('Server started on port 3000.');
